package com.yay.rental.ui;

import com.yay.rental.R;
import com.yay.rental.provider.CommentsService;
import com.yay.rental.provider.RemoteRentalProvider;
import com.yay.rental.provider.RentalContract.Rental;
import com.yay.rental.util.UIUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * Get Started!
 * @author team@getstarted.com.ua
 * 
 * {@link Activity} that displays details about a specific
 * {@link Rental#_ID}, as requested through {@link Intent#getData()}.
 */
public class RentalDetailActivity extends Activity {
    //private static final String TAG = "RentalDetailActivity";
    private Uri mRentalUri;
    private TextView mName;
    private TextView mId;
    private TextView mPrice;
    private TextView mNumReviews;
    private View mIcon;
    private Gallery mGallery;
    private TextView mAccommodations;
    //private TextView mDescription;
    private model.Rental mRental;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rental_detail);
        
        mName = (TextView) findViewById(R.id.rental_name);
        mId = (TextView) findViewById(R.id.rental_id);
        mPrice = (TextView) findViewById(R.id.rental_price);
        mNumReviews = (TextView) findViewById(R.id.rental_num_reviews);
        mIcon = (View) findViewById(R.id.icon);
        mGallery = (Gallery) findViewById(R.id.gallery);
        mAccommodations = (TextView) findViewById(R.id.accommodations);
        //mDescription = (TextView) findViewById(R.id.description);

        final Intent intent = getIntent();
        mRentalUri = intent.getData();
        
        mRental = new RemoteRentalProvider().findById(Rental.getRentalId(mRentalUri));
        mName.setText(mRental.getName());
        mId.setText(getResources().getString(R.string.property_num) + mRental.getId());
        String num_reviews = new CommentsService().numComment(CommentsService.getCurrentComments().getRentalId());
        StringBuilder price = new StringBuilder(16);
        price.append(mRental.getPrice_min()).append("-").
        append(mRental.getPrice_max()).append(" ").
        append(mRental.getCurency()).append("/").
        append(mRental.getUnit());
        mPrice.setText(price.toString());
        mNumReviews.setText(num_reviews.trim());
        
        UIUtils.fetchDrawableOnThread(mRental.getThumb(), mIcon);
        
        mGallery.setAdapter(new ImagesAdapter(RentalDetailActivity.this));
            mGallery.setOnItemClickListener(new OnItemClickListener() {
            	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            		UIUtils.fetchDrawableOnThread(mRental.getPictures().get(position), mIcon);
                }
            });
            
        StringBuilder acc = new StringBuilder(16);
        acc.append(mRental.getBedrooms()).append(" ").append(getResources().getString(R.string.btn_filter_bedrooms).toLowerCase()).append(", ").
        append(mRental.getBathroom()).append(" ").append(getResources().getString(R.string.btn_filter_bathrooms).toLowerCase()).append(" ");
        mAccommodations.setText(acc.toString());   
        
        //mDescription.setText(mRental.getDescription());
    }

    private static String tjpg = "_t.jpg";
    private static String jpg = ".jpg";
    class ImagesAdapter extends BaseAdapter {
    	int GalItemBg;
	    private Context mContext;
	    
	    public ImagesAdapter(Context c) {
	        mContext = c;
	        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
	    }
	
	    public int getCount() {
	        return mRental.getPictures().size();
	    }
	
	    public Object getItem(int position) {
	        return mRental.getPictures().get(position);
	    }
	
	    public long getItemId(int position) {
	        return position;
	    }
	
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	ImageView i = new ImageView(mContext);
	    	String url = mRental.getPictures().get(position);
	    	if(url.indexOf(tjpg) < 0) { 
	    		UIUtils.fetchDrawableOnThread(url.replace(jpg, tjpg), i);
    		}
	    	i.setLayoutParams(new Gallery.LayoutParams(100, 90));
	        i.setScaleType(ImageView.ScaleType.FIT_XY);
	        i.setBackgroundResource(GalItemBg);
	        return i;
	    }
	

	}
    
    /** Handle "contact the owner" action. */
    public void onContactClick(View v) {
    	// TODO
    }

    /** Handle "share" action. */
    public void onShareClick(View v) {
        String shareString = getResources().getString(R.string.share_template, mRental.getName());
        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, shareString);
        startActivity(Intent.createChooser(intent, getText(R.string.title_share)));
    }
    /** Handle "comments" title-bar action. */
    public void onCommentsClick(View v) {
    	final Intent intent = new Intent(RentalDetailActivity.this, CommentsActivity.class);
    	intent.putExtra("rentalId", mRental.getId());
        startActivity(intent);
    }

}
